package april;

/**
 * @author ：冯涛滔
 * @date ：Created in 2020-4-20 20:48
 * @description：
 * @modified By：
 * @version:
 */
public class April20 {
    public static void main(String[] args) {
        System.out.println(numIslands());
    }
    public static int numIslands() {
//        char[][] grid = {{'1','1','1'},{'0','1','1'},{'1','1','1'}};
        char[][] grid = {{'1','1','1','1','0'},{'1','1','0','1','0'},{'1','1','0','0','0'},{'0','0','0','0','0'}};
        int count = 0;
        for (int i = 0; i < grid.length ; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                if (grid[i][j] == '1') {
                    count++;
                    check(grid, i, j);
                }
            }
        }

        return count;
    }
    //以某个节点为点 查找它的左边的下边是否存在陆地
    public static void check(char[][] grid,int i,int j){
        if(!(i < grid.length)||i==-1){
            return;
        }
        if(!(j< grid[i].length)||j==-1) {
            return;
        }

        if(grid[i][j]=='1'){//如果这是一块陆地
            grid[i][j] = (char) 0;
            check(grid,i,j+1); //查看它右边是不是陆地
            check(grid, i+1,j); //查看它下面是不是陆地
            check(grid,i,j-1);// 左边
            check(grid,i-1,j);
        }

    }
}
